<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:chrome="clr-namespace:DropDownButtonLib.Chromes"
                    xmlns:conv="clr-namespace:DropDownButtonLib.Core.Converters"
                    xmlns:local="clr-namespace:DropDownButtonLib.Controls"
                    xmlns:themes="clr-namespace:DropDownButtonLib.Core.Themes"
                    >

   <conv:InverseBoolConverter x:Key="InverseBoolConverter" />

   <LinearGradientBrush x:Key="PopupDarkBorderBrush" EndPoint="0.5,1" StartPoint="0.5,0">
      <GradientStop Color="#FFA3AEB9" Offset="0" />
      <GradientStop Color="#FF8399A9" Offset="0.375" />
      <GradientStop Color="#FF718597" Offset="0.375" />
      <GradientStop Color="#FF617584" Offset="1" />
   </LinearGradientBrush>

   <LinearGradientBrush x:Key="PopupBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
      <LinearGradientBrush.GradientStops>
         <GradientStopCollection>
            <GradientStop Offset="0" Color="#FFffffff" />
            <GradientStop Offset="1" Color="#FFE8EBED" />
         </GradientStopCollection>
      </LinearGradientBrush.GradientStops>
   </LinearGradientBrush>

  <Style TargetType="{x:Type local:DropDownItemsButton}">
      <Setter Property="BorderThickness" Value="1" />
      <Setter Property="IsTabStop" Value="False" />
      <Setter Property="HorizontalContentAlignment" Value="Center" />
      <Setter Property="VerticalContentAlignment" Value="Center" />
      <Setter Property="Padding" Value="3" />
      <Setter Property="TemplateOfItemControl">
      <Setter.Value>
        <ControlTemplate TargetType="ItemsControl">
          <ItemsPresenter/>
        </ControlTemplate>
      </Setter.Value>
      </Setter>
      <Setter Property="ItemsPanel">
        <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
      </Setter.Value>
      </Setter>
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DropDownItemsButton}">
            <Grid x:Name="MainGrid" SnapsToDevicePixels="True">
                  <ToggleButton x:Name="PART_DropDownButton"
                                Grid.Column="1"
                                IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                IsHitTestVisible="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
                                >
                     <ToggleButton.Template>
                        <ControlTemplate TargetType="ToggleButton">
                           <ContentPresenter />
                        </ControlTemplate>
                     </ToggleButton.Template>
                     <Grid>
                        <chrome:ButtonChrome x:Name="ToggleButtonChrome"
                                             CornerRadius="2.75"
                                             RenderChecked="{TemplateBinding IsOpen}"
                                             RenderEnabled="{TemplateBinding IsEnabled}"
											                       RenderMouseOver="{Binding IsMouseOver, ElementName=PART_DropDownButton}"
											                       RenderPressed="{Binding IsPressed, ElementName=PART_DropDownButton}"
                                             >
                           <Grid>
                              <Grid.ColumnDefinitions>
                                 <ColumnDefinition Width="*" />
                                 <ColumnDefinition Width="Auto" />
                              </Grid.ColumnDefinitions>
                              <ContentPresenter Margin="{TemplateBinding Padding}"
                                                Content="{TemplateBinding Content}"
                                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                RecognizesAccessKey="true"
                                                />
                              <Grid x:Name="arrowGlyph" IsHitTestVisible="False" Margin="4,3,4,3" Grid.Column="1">
                                 <Path x:Name="Arrow"
                                       Width="7"
                                       Height="4"
                                       Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z"
                                       Fill="{DynamicResource {x:Static themes:ResourceKeys.GlyphNormalForegroundKey}}" />
                              </Grid>
                           </Grid>
                        </chrome:ButtonChrome>
                     </Grid>
                  </ToggleButton>

                  <Popup x:Name="PART_Popup" 
                         HorizontalOffset="1"
                         VerticalOffset="1"
                         AllowsTransparency="True"
                         StaysOpen="False"
                         Placement="Bottom"
                         Focusable="False"
                         MaxHeight="{Binding Path=DropDownMaxHeight, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}"
                         MaxWidth="{Binding Path=DropDownMaxWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}"
                         IsOpen="{Binding IsChecked, ElementName=PART_DropDownButton}">
                    <Border BorderThickness="1"
                            Background="{DynamicResource {x:Static themes:ResourceKeys.ControlNormalBackgroundKey}}"
                            BorderBrush="{DynamicResource {x:Static themes:ResourceKeys.ControlNormalBorderKey}}">
                      <ScrollViewer VerticalScrollBarVisibility="Auto"
                                    CanContentScroll="True"
                                    HorizontalAlignment="Left"
                                    Margin="0">
                        <ItemsControl x:Name="PART_ItemsControl"
                                      ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}"
                                      ItemTemplate="{Binding Path=ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}" 
                                      ItemContainerStyle="{Binding Path=ItemContainerStyle, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}" 
                                      ItemContainerStyleSelector="{Binding Path=ItemContainerStyleSelector, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}"
                                      ItemsPanel="{Binding Path=ItemsPanel, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}"
                                      Template="{Binding Path=TemplateOfItemControl, RelativeSource={RelativeSource FindAncestor, AncestorType=local:DropDownItemsButton, AncestorLevel=1}}"
                                      />
                      </ScrollViewer>
                    </Border>
                  </Popup>

               </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                      <Setter Property="Fill" TargetName="Arrow" Value="{DynamicResource {x:Static themes:ResourceKeys.GlyphDisabledForegroundKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
         </Setter.Value>
      </Setter>
  </Style>

</ResourceDictionary>
